package com.db.client.gui;

import java.net.InetAddress;
import java.util.logging.Logger;

import com.db.client.Client;
import com.db.protocol.tags.OrderSide;

public class GuiControler implements GuiListener {
	
	private static Logger log = Logger.getLogger(GuiControler.class.getName());
	
	private Client client;
	
	private GuiView guiView;
	
	private final InetAddress serverAddress;
	
	private final int serverPort;
	
	public GuiControler(InetAddress serverAddress, int serverPort) {
		this.serverAddress = serverAddress;
		this.serverPort = serverPort;
		guiView = new MainClient();
		guiView.setListener(this);
	}

	public void runGui() {
		MainClient.runGui(guiView);
	}
	
	@Override
	public void connect(String clientID) {
		log.info("From gui: connect client [" + clientID + "]");
		client = new Client(clientID, guiView);
		
		client.connect(serverAddress, serverPort);
	}

	public boolean connected() {
		return client != null;
	}
	
	@Override
	public void createOrder(String symbolTicket, OrderSide side, int count) {
		log.info("From gui: create market order");
		client.createOrder(symbolTicket, side, count);		
	}

	@Override
	public void createOrder(String symbolTicket, OrderSide side, float price,
			int count) {
		log.info("From gui: create limit order");
		client.createOrder(symbolTicket, side, price, count);
	}

	@Override
	public void cancelOrder(String orderID) {
		log.info("From gui: cancel order [" + orderID + "]");
		client.cancelled(orderID);		
	}

}
